<?php
namespace app\adminapi\controller\v1\user;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\user\UserBillServices;
use app\services\user\UserGroupServices;
use app\services\user\UserServices;
use think\facade\App;

/**
 * 会员积分管理设置
 * Class UserIntegral
 * @package app\admin\controller\user
 */
class UserIntegral extends AuthController
{
    /**
     * @var UserServices
     */
    protected $userServices;

    /**
     * user constructor.
     * @param App $app
     * @param UserGroupServices $services
     */
    public function __construct(App $app, UserBillServices $services,UserServices $userServices)
    {
        parent::__construct($app);
        $this->services = $services;
        $this->userServices = $userServices;
    }



    public function addIntegral(Request $request)
    {
        $data = $request->postMore([
            ['type',1],
            ['uid',0],
            ['integration',0],
        ]);
        $user = $this->userServices->get($data['uid']);
        
        $integral_data = ['link_id' => $request->adminId(), 'number' => $data['integration']];
        if ($data['type'] == 1) {//增加
            $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
            $integral_data['balance'] = $edit['integral'];
            $integral_data['title'] = '系统增加积分';
            $integral_data['mark'] = '系统增加了' . floatval($data['integration']) . '积分';
            $res2 = $this->services->incomeIntegral($user['uid'], 'system_add', $integral_data);
        }
        if ($data['type'] == 2) {//减少
            $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
            $integral_data['balance'] = $edit['integral'];
            $integral_data['title'] = '系统减少积分';
            $integral_data['mark'] = '系统扣除了' . floatval($data['integration']) . '积分';
            $res2 = $this->services-->expendIntegral($user['uid'], 'system_sub', $integral_data);
        }
        return app("json")->success("操作成功");
    }

}